import math
import numpy as np
# 加载数据,及数据预处理
trainData = []
trainLabel = []
testData = []
with open('AI_homework.txt') as fileHandle:
    dataLine = fileHandle.readlines()
    for sample in dataLine:
        sampleArr = np.array(sample.split('\t'), dtype=int)
        # 数据预处理
        if sampleArr[0] < 16:
            # 训练数据
            trainData.append(sampleArr[1:-1])
            trainLabel.append(sampleArr[-1])
        else:
            testData.append(sampleArr[1:-1])
# 训练:获取条件概率向量:p(X|wi),p(wi);1:男
trainData = np.array(trainData)
trainLabel = np.array(trainLabel)
seg1_trainData = trainData[trainLabel == 1]
seg0_trainData = trainData[trainLabel == 0]
measH_seg1 = np.mean(seg1_trainData[:, 0])
measW_seg1 = np.mean(seg1_trainData[:, 1])
measH_seg0 = np.mean(seg0_trainData[:, 0])
measW_seg0 = np.mean(seg0_trainData[:, 1])

sdH_seg1 = np.std(seg1_trainData[:, 0])
sdW_seg1 = np.std(seg1_trainData[:, 1])
sdH_seg0 = np.std(seg0_trainData[:, 0])
sdW_seg0 = np.std(seg0_trainData[:, 1])

total_num = len(trainData)
pw1=len(seg1_trainData)/float(total_num)
pw0=len(seg0_trainData)/float(total_num)
# 预测
result = []


def NP(x, u, v):
    # 功能：
    # 参数：
    # return:
    return (1 / ((2 * math.pi)**0.5 * v)) * math.exp(-(x - u)**2 / (2 * v**2))


for sample in testData:
    P1 = pw1*NP(sample[0],measH_seg1,sdH_seg1)*NP(sample[1],measW_seg1,sdW_seg1)
    P0 = pw0*NP(sample[0],measH_seg0,sdH_seg0)*NP(sample[1],measW_seg0,sdW_seg0)
    if P1 > P0:
        result.append((1))
    else:
        result.append(0)
print (result)
""",数值离散化的处理方式
import numpy as np
# 加载数据,及数据预处理
trainData = []
trainLabel = []
testData = []
min_high = 120
max_high = 250
step_high = 5
min_weight = 40
max_weight = 200
step_weight = 2
with open('AI_homework.txt') as fileHandle:
    dataLine = fileHandle.readlines()
    for sample in dataLine:
        sampleArr = np.array(sample.split('\t'), dtype=int)
        # 数据预处理
        sampleArr[1] = (sampleArr[1] - min_high) / step_high
        sampleArr[2] = (sampleArr[2] - min_weight) / step_weight
        if sampleArr[0] < 16:
            # 训练数据
            trainData.append(sampleArr[1:-1])
            trainLabel.append(sampleArr[-1])
        else:
            testData.append(sampleArr[1:-1])
# 训练:获取条件概率向量:p(X|wi),p(wi);1:男
num_hightLevel = int((max_high - min_high) / step_high)
num_weightLevel =int( (max_weight - min_weight) / step_weight )
PXh_w1 = np.zeros(num_hightLevel)
PXh_w0 = np.zeros(num_hightLevel)
PXw_w1 = np.zeros(num_weightLevel)
PXw_w0 = np.zeros(num_weightLevel)
w1_num = 0
total_num = len(trainData)
for i in range(total_num):
    if trainLabel[i] == 1:
        w1_num += 1
        highLevel = trainData[i][0]
        PXh_w1[highLevel] += 1
        weightLevel = trainData[i][1]
        PXw_w1[weightLevel] += 1
    else:
        highLevel = trainData[i][0]
        PXh_w0[highLevel] += 1
        weightLevel = trainData[i][1]
        PXw_w0[weightLevel] += 1
w0_num = total_num - w1_num
# 训练结果(所得知识)
PXh_w1 /= float(w1_num)
PXh_w0 /= float(w0_num)
PXw_w1 /= float(w1_num)
PXw_w0 /= float(w0_num)
pW1 = w1_num / float(total_num)
# 预测
result=[]
for sample in testData:
    P1=pW1*PXh_w1[sample[0]]*PXw_w1[sample[1]]
    P0=(1-pW1)*PXh_w0[sample[0]]*PXw_w0[sample[1]]
    if P1>P0:
        result.append((1))
    else:
        result.append(0)
"""
